<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input v-model="queryInfo.query" placeholder="请输入内容" clearable class="input-with-select" @clear="fetchData()">
            <el-button slot="append" icon="el-icon-search" @click="fetchData()" />
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column label="订单编号" prop="orderNum" />
        <el-table-column label="就餐方式" prop="type" width="80px" />
        <el-table-column label="顾客名字" prop="customerName" width="80px" />
        <el-table-column label="顾客电话" prop="customerPhone" width="100px" />
        <el-table-column label="订单价格" prop="orderCost" width="80px" />
        <el-table-column label="下单时间" prop="orderTimestart" width="160px" />
        <el-table-column label="结算时间" prop="orderTimefinish" width="160px" />
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeOrderById(scope.row.orderId)" />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="queryInfo.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import { getOrderList, deleteOrderById } from '@/api/order'

export default {
  data() {
    return {
      orderList: [],
      listLoading: true,
      queryInfo: {
        query: '',
        page: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async  fetchData() {
      const result = await getOrderList(this.queryInfo)
      if (result.code == 200) {
        this.orderList = result.data.rows
        this.total = result.data.total
        console.log(result.data.rows)
      }
    },
    // 处理页码的改变的数据请求
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.fetchData()
    },
    // 当前页码的改变
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
      this.fetchData()
    },
    removeOrderById(id) {
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const result = await deleteOrderById(id)
          if (result.code == 200) {
            this.$message({
              message: '订单删除成功！',
              type: 'success' })
            this.fetchData()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }

}
</script>
